package com.andyliu.system.controller;


import com.andyliu.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.andyliu.system.service.*;
import com.andyliu.system.entity.*;
import java.util.List;
import java.util.Arrays;

import org.springframework.ui.ModelMap;
import com.andyliu.controller.BaseController;
import com.andyliu.common.ResponseResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import springfox.documentation.annotations.ApiIgnore;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import com.andyliu.controller.BaseController;

/**
 * <p>
 * 用户和角色关联表 前端控制器
 * </p>
 *
 * @author andyLiu
 * @since 2021-08-10
 */
@Slf4j
@Controller
@RequestMapping("/system/user-role")
@Api(value = "用户和角色关联表控制器",tags = "用户和角色关联表操作接口")
    public class UserRoleController extends BaseController {
    @Autowired
    private IUserRoleService service;
    /**
    * 跳转到列表页
    */
    @GetMapping()
    @ApiIgnore
    public String toData(){
        log.debug("跳转到列表页");
        return"system/UserRole/data";
    }
    /**
    * 异步加载分页数据
    */
    @PostMapping("data")
    @ResponseBody
    @ApiOperation(value = "用户和角色关联表分页数据接口",notes = "根据查询和分页条件返回分页数据",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "分页页码",required = true,type = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示个数",required = true,type = "Integer"),
            @ApiImplicitParam(name="obj",value="用于分装实体的查询条件",required = false)
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    public ResponseResult data(UserRole obj,@RequestParam(required = true,defaultValue = "1") Integer pageNumber,@RequestParam(required = true,defaultValue = Constants.DEFAULT_PAGE_SIZE) Integer pageSize){
            log.debug("分页查询UserRole数据");
            IPage<UserRole> page=service.selectByPage(pageNumber,pageSize,obj);
            return ResponseResult.success(page);
    }
    /**
    * 跳转到新增编辑页面
    */
    @GetMapping("/edit/{id}")
    @ApiIgnore
    public String toEidt(@PathVariable Integer id,ModelMap mmap){
        if(id!=-1){
            UserRole userRole=service.getById(id);
            mmap.put("userRole" ,userRole);
        }
        return"system/UserRole/edit";
    }
    @GetMapping("/info/{id}")
    @ResponseBody
    @ApiOperation(value = "根据主键获取用户和角色关联表数据接口",notes = "根据主键获取实体数据",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    public ResponseResult toEidt(@PathVariable Integer id){
        log.debug("查看用户和角色关联表的数据,id为"+id);
        UserRole obj = service.getById(id);
        return ResponseResult.success(obj);
    }

    /**
    * 异步保存或新增
    */
    @PostMapping("save")
    @ResponseBody
    @ApiOperation(value = "保存或更新用户和角色关联表数据接口",notes = "如果实体类中有主键为更新，否则为保存",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户信息表实体",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    public ResponseResult save(UserRole userRole){
        log.debug("保存用户和角色关联表的数据");
        if(userRole.getUserId()!=null){
            boolean status=service.save(userRole);
            if(status){
            return ResponseResult.success();
            }else{
            return ResponseResult.error();
            }
        }
        return service.updateById(userRole)==true?
        ResponseResult.success()
        :ResponseResult.error();
    }
    /**
    * 删除或批量删除请求
    */
    @DeleteMapping("delete")
    @ResponseBody
    @ApiOperation(value = "批量删除用户和角色关联表数据接口",notes = "批量删除",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "需要删除的主键的数组",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    public boolean delete(Integer[] ids){
        log.debug("删除用户和角色关联表的数据,id为"+ids);
        return service.removeByIds(Arrays.asList(ids));
    }
    /**
    * 单个删除
    */
    @DeleteMapping("delete/{id}")
    @ResponseBody
    @ApiOperation(value = "删除用户和角色关联表数据接口",notes = "单个删除，根据主键删除",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要删除主键",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    public boolean delete(@PathVariable Integer id){
        log.debug("删除用户和角色关联表的数据,id为"+id);
        return service.removeById(id);
    }

}


